package com.ycx.ycxmybatismanagement.controller;

import com.ycx.ycxmybatismanagement.dto.AddEmpDTO;
import com.ycx.ycxmybatismanagement.dto.EmpListDTO;
import com.ycx.ycxmybatismanagement.dto.UpdateEmpDTO;
import com.ycx.ycxmybatismanagement.pojo.PageBean;
import com.ycx.ycxmybatismanagement.pojo.Result;
import com.ycx.ycxmybatismanagement.service.EmpService;
import com.ycx.ycxmybatismanagement.vo.EmpListVo;
import com.ycx.ycxmybatismanagement.vo.EmpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/emps")
public class EmpController {
    /**
     * 传入  页码  展示数据的条数
     * 分页:
     *    返回  总记录的条数   当前页的数据
     */
    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param empListDTO
     * @return
     */
    @GetMapping()
    public Result<PageBean<EmpListVo>> emps(EmpListDTO empListDTO){
        PageBean<EmpListVo>  empPageList = empService.empPageList(empListDTO);
        return Result.success(empPageList);
    }

    /**
     * 添加
     * @param empDTO
     * @return
     */
    @PostMapping
    public Result addEmp(@RequestBody AddEmpDTO empDTO){
        empService.addEmp(empDTO);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteEmps(@RequestParam("ids")List<Integer> ids){
        empService.deleteEmps(ids);
        return Result.success();
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<EmpVO> selectById(@PathVariable("id") Integer id){
       EmpVO empVO= empService.selectById(id);
       return Result.success(empVO);
    }
    @PutMapping
    public Result updateEmp(@RequestBody UpdateEmpDTO updateEmpDTO){
        empService.updateEmp(updateEmpDTO);
        return Result.success();
    }

    @GetMapping("list")
    public Result<List<EmpListVo>> empsList(){
        List<EmpListVo> empListVO = empService.empsList();
        return Result.success(empListVO);
    }
}
